package com.server.user;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.aliyuncs.http.HttpRequest;
import com.config.ConstService;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.util.StrKit;

/**
 * APP用户相关的basectrl
 * @author dell
 *
 */
@Controller
public class AppUserBaseCtrl {
	@Resource
	HttpServletRequest req;
	@Autowired
	ConstService constService;
	@Autowired
	UserService userService;
	
	static HttpSession session;
	
	public int pageSize_default=10;//默认每页大小
	public int pageNumber;
	public int pageSize;
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public HttpSession getSession()
	{
		session=req.getSession();
		return session;
	}
	public HttpServletRequest getReq()
	{
		return req;
	} 
	/**
	 * 刷新登录用户信息
	 */
	public void refreshUser()
	{
		Map<String, Object> user =  (Map<String, Object>) getSession().getAttribute("wxuser");	
		String userid = (String)user.get("id");
		String openid = (String)user.get("openid");
		Map<String, Object> usernew =userService.getUserById(userid);
		usernew.put("openid", openid);
		getSession().setAttribute("wxuser", usernew);
	}
	
	/**
	 * 获取登陆用户信息
	 * @return
	 */
	public Map<String, Object> getLogUser()
	{
		return (Map<String, Object>) getSession().getAttribute("wxuser");			
	}


	/**
	 * 获取当前页
	 * @return
	 */
	public int getPN()
	{
		String pn=req.getParameter("pageNumber");
		if(StrKit.notBlank(pn))
		{
			pageNumber=Integer.parseInt(pn);
		}else
		{
			pageNumber=1;
		}
		return pageNumber;
	}
	/**
	 * 获取页面大小
	 * @return
	 */
	public int getPS()
	{
		String ps=req.getParameter("pageSize");
		if(StrKit.notBlank(ps))
		{
			pageSize=Integer.parseInt(ps);
		}else
		{
			pageSize=pageSize_default;
		}
		return pageSize;
	}
	
	/**
	 * 将请求数据封装成MAP
	 * @return
	 */
	public Map<String, Object> getReqMap()
	{
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> enu = getReq().getParameterNames();
		if(enu!=null)
		{
			while(enu.hasMoreElements())
			{
				String key = enu.nextElement();
				map.put(key, getReq().getParameter(key));
			}
		}
		
		return map;
	}
	
	/**
	 * 获取Body传参
	 * @return
	 */
	public String getBodyData()
	{
		StringBuilder sb = new StringBuilder();
		try {
			BufferedReader streamReader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
			// 写入数据到Stringbuilder
	        
	        String line = null;
	        while ((line = streamReader.readLine()) != null) {
	            sb.append(line);
	        }
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("接受到消息:"+sb.toString());
		return sb.toString();
	}
}
